package com.glsc.ngateway.platform.service.it;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItArpDevice;
import com.glsc.ngateway.common.api.platform.dto.it.SwitchBoardExport;
import com.glsc.ngateway.common.base.domainmapper.SwitchBoardRowMapper;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItArpDeviceRepo;
import com.glsc.ngateway.platform.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SwitchService {

    Logger logger = LoggerFactory.getLogger(SwitchService.class);

    @Autowired
    private UserService userService;

    @Autowired
    private ItArpDeviceRepo itArpDeviceRepo;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void truncateSwitchData() {
        itArpDeviceRepo.truncateTableItArpDevice();
    }

    public List<ItArpDevice> findAllList() {
        List<ItArpDevice> list = itArpDeviceRepo.findAll();
        return list;
    }

    public void saveSwitch(ItArpDevice itArpDevice) {
        itArpDeviceRepo.save(itArpDevice);
    }

    public List<ItArpDevice> findSwitch(String switchIp, String switchInterface, String macAddress) {
        List<ItArpDevice> findSwitch = itArpDeviceRepo.findBySwitchIpAndSwitchInterfaceAndMacAddress(switchIp, switchInterface, macAddress);
        return findSwitch;
    }


    //    //导出服务器交换机对应表
    public List<SwitchBoardExport> exportSwitchBoardOutPut() {
        String sql = "select a.switch_ip,a.switch_interface,a.mac_address,c.op_name business_name,d.mx_name application_detail,g.name, "
                + "e.dict_value machine_room,machine_cab,b.machine_unit,b.virtual_name,b.virtual_ip,ip1,ip2 from t_it_arp_device a join "
                + "(select mac1,mac2,mac3,mac4,mac5,mac6,'' as virtual_name,'' as virtual_ip, "
                + "business_name,application_detail,machine_room,machine_cab,machine_unit,ip1,ip2,user_id from t_it_server_device "
                + "union all "
                + "select i.mac1,i.mac2,'','','','',i.virtual_name,h.ip1 virtual_ip, "
                + "i.business_name,i.application_detail,'','','',i.ip1,i.ip2,i.user_id from t_it_virtual_device i left join t_it_server_device h on h.dev_id=i.virtual_ip) b "
                + "on (a.mac_address = b.mac1) or (a.mac_address = b.mac2) or (a.mac_address = b.mac3) "
                + "or (a.mac_address = b.mac4) or (a.mac_address = b.mac5)  or (a.mac_address = b.mac6) "
                + "left join t_gw_system_main c on b.business_name = c.system_class_no "
                + "left join t_it_application_detail d on b.application_detail = d.mx_id "
                + "left join t_it_dict e on b.machine_room = e.zd_id "
//				   + "left join t_dict f on b.machine_cab = f.zd_id "
                + " left join t_user g on b.user_id = g.userid where 1=1 ";
        return jdbcTemplate.query(sql, new SwitchBoardRowMapper());
    }
//    public List<SwitchBoardExport> querySwitchBoardOutPut(String switchRecord) {
//        String condition = "";
//        if (switchRecord != null) {
//            condition += " and switch_ip like '%" + switchRecord + "%' or ip1 like '%" + switchRecord + "%' or ip2 like '%" + switchRecord + "%' "
//                    + "or mac1 like '%" + switchRecord + "%' or mac2 like '%" + switchRecord + "%' or mac3 like '%" + switchRecord + "%' "
//                    + "or mac4 like '%" + switchRecord + "%' or virtual_ip like '%" + switchRecord + "%' ";
//        }
////			if(machine!=null) {
////				condition+=" and machine="+machine;
////			}
//        String sql = "select a.switch_ip,a.switch_interface,a.mac_address,c.op_name business_name,d.mx_name application_detail,g.user_name user_id, "
//                + "e.dict_value machine_room,machine_cab,b.machine_unit,b.virtual_name,b.virtual_ip,ip1,ip2 from t_arp_device a join "
//                + "(select mac1,mac2,mac3,mac4,mac5,mac6,'' as virtual_name,'' as virtual_ip, "
//                + "business_name,application_detail,machine_room,machine_cab,machine_unit,ip1,ip2,user_id from t_service_device "
//                + "union all "
//                + "select i.mac1,i.mac2,'','','','',i.virtual_name,h.ip1 virtual_ip, "
//                + "i.business_name,i.application_detail,'','','',i.ip1,i.ip2,i.user_id from t_virtual_device i left join t_service_device h on h.dev_id=i.virtual_ip) b "
//                + "on (a.mac_address = b.mac1) or (a.mac_address = b.mac2) or (a.mac_address = b.mac3) "
//                + "or (a.mac_address = b.mac4) or (a.mac_address = b.mac5)  or (a.mac_address = b.mac6) "
//                + "left join t_operation c on b.business_name = c.op_id "
//                + "left join t_yinyong_detail d on b.application_detail = d.mx_id "
//                + "left join t_dict e on b.machine_room = e.zd_id "
////				   + "left join t_dict f on b.machine_cab = f.zd_id "
//                + " left join t_admin_user g on b.user_id = g.user_id where 1=1 " + condition;
//        return jdbcTemplate.query(sql, new SwitchBoardRowMapper());
//    }
//
//    //搜索服务器交换机对应表
//    public List<Map<String, Object>> searchSwitchBoard(String switchRecord) {
//        String sql = "select * from t_arp_device a join "
//                + " (select mac1,mac2,mac3,mac4,mac5,mac6,'' as virtual_name,'' as virtual_ip, " +
//                "business_name,application_detail,machine_room,machine_cab,machine_unit,ip1,ip2,user_id from t_service_device " +
//                "union all " +
//                "select c.mac1,c.mac2,'','','','',c.virtual_name,d.ip1 virtual_ip, " +
//                "c.business_name,c.application_detail,'','','',c.ip1,c.ip2,c.user_id " +
//                "from t_virtual_device c " +
//                "left join t_service_device d on d.dev_id = c.virtual_ip) b on (a.mac_address = b.mac1) or (a.mac_address = b.mac2) or (a.mac_address = b.mac3) "
//                + "or (a.mac_address = b.mac4) or (a.mac_address = b.mac5)  or (a.mac_address = b.mac6) "
//                + "where switch_ip like '%" + switchRecord + "%' or ip1 like '%" + switchRecord + "%' or ip2 like '%" + switchRecord + "%' or mac1 like '%" + switchRecord + "%' "
//                + "or mac2 like '%" + switchRecord + "%' or mac3 like '%" + switchRecord + "%' or mac4 like '%" + switchRecord + "%' or virtual_ip like '%" + switchRecord + "%' ";
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql));
//    }
//
//    //搜索服务器交换机名称
//    public List multiSearch(Map<String, Object> switchBoardInfo) {
//        String condition = " where  1=1 ";
//        String switchIp = (String) switchBoardInfo.get("switchIp");
//        if (!"".equals(switchIp) && switchIp != null) {
//            condition += " and switch_ip = '" + switchIp + "'";
//        }
//        String switchInterface = (String) switchBoardInfo.get("switchInterface");
//        if (!"".equals(switchInterface) && switchInterface != null) {
//            condition += " and switch_interface = '" + switchInterface + "'";
//        }
//        String macAddress = (String) switchBoardInfo.get("macAddress");
//        if (!"".equals(macAddress) && macAddress != null) {
//            condition += " and mac_address = '" + macAddress + "'";
//        }
//        Integer machineRoom = (Integer) switchBoardInfo.get("machineRoom");
//        if (machineRoom != null) {
//            condition += " and machine_room = " + machineRoom.intValue();
//        }
//        String machineCab = (String) switchBoardInfo.get("machineCab");
//        if (!"".equals(machineCab) && machineCab != null) {
//            condition += " and machine_cab = '" + machineCab + "'";
//        }
//        String virtualName = (String) switchBoardInfo.get("virtualName");
//        if (!"".equals(virtualName) && virtualName != null) {
//            condition += " and virtual_name like '%" + virtualName + "%'";
//        }
//        String ip1 = (String) switchBoardInfo.get("ip1");
//        if (!"".equals(ip1) && ip1 != null) {
//            condition += " and ip1 = '" + ip1 + "'";
//        }
//        String ip2 = (String) switchBoardInfo.get("ip2");
//        if (!"".equals(ip2) && ip2 != null) {
//            condition += " and ip2 = '" + ip2 + "'";
//        }
//        String virtualIp = (String) switchBoardInfo.get("virtualIp");
//        if (!"".equals(virtualIp) && virtualIp != null) {
//            condition += " and virtual_ip like '%" + virtualIp + "%'";
//        }
//        String businessName = (String) switchBoardInfo.get("businessName");
//        if (!"".equals(businessName) && businessName != null) {
//            condition += " and business_name = '" + businessName + "'";
//        }
//        String application = (String) switchBoardInfo.get("application");
//        if (!"".equals(application) && application != null) {
//            condition += " and application = '" + application + "'";
//        }
//        String userId = (String) switchBoardInfo.get("userId");
//        if (!"".equals(userId) && userId != null) {
//            condition += " and user_Id = '" + userId + "'";
//        }
//        String sql = "select * from t_arp_device a join " +
//                "(select mac1,mac2,mac3,mac4,mac5,mac6,'' as virtual_name,'' as virtual_ip," +
//                "business_name,application_detail,machine_room,machine_cab,machine_unit,ip1,ip2,user_id from t_service_device " +
//                "union all " +
//                "select c.mac1,c.mac2,'','','','',c.virtual_name,d.ip1 virtual_ip,c.business_name, " +
//                "c.application_detail,'','','',c.ip1,c.ip2,c.user_id from t_virtual_device c " +
//                "left join t_service_device d on d.dev_id = c.virtual_ip) b on (a.mac_address = b.mac1) or (a.mac_address = b.mac2) or (a.mac_address = b.mac3) " +
//                "or (a.mac_address = b.mac4) or (a.mac_address = b.mac5)  or (a.mac_address = b.mac6) " + condition;
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql));
//    }


    public Page<Map<String, Object>> searchSwitchBoard(Integer page, Integer size, String businessName, String applicationDetail,
                                                       String virtualName, String ip1, String ip2, Integer machineRoom,
                                                       String machineCab, String switchIp, String switchInterface,
                                                       String macAddress, Integer virtualIp, String userId) {
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<Map<String, Object>> list = itArpDeviceRepo.searchSwitchBoard(pageable, businessName, applicationDetail,
                virtualName, ip1, ip2, machineRoom, machineCab, switchIp, switchInterface, macAddress, virtualIp, userId);
        return list;
    }


}
